import { defineStore } from 'pinia'
import type { CategoryStore } from '@/api/product/model/categoryModel'
import {
  reqGetCategory1ListData,
  reqGetCategory2ListData,
  reqGetCategory3ListData,
} from '@/api/product/category'

export const useCategoryStore = defineStore('categoryList', {
  state: (): CategoryStore => {
    return {
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },
  actions: {
    async getCategory1ListData() {
      this.category1Id = undefined
      this.category2Id = undefined
      this.category3Id = undefined
      this.category2List = []
      this.category3List = []
      const result = await reqGetCategory1ListData()
      this.category1List = result
    },
    async getCategory2ListData(category1Id: number) {
      this.category1Id = category1Id
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
      const result = await reqGetCategory2ListData(category1Id)
      this.category2List = result
    },
    async getCategory3ListData(category2Id: number) {
      this.category2Id = category2Id
      this.category3Id = undefined
      const result = await reqGetCategory3ListData(category2Id)
      this.category3List = result
    },
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id
    },
  },
})
